package com.yujinglin.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yujinglin.airline.airlineWritable; // 假设这个类存在，并且有一个接受字符串的构造函数
import com.yujinglin.writable.airclassify;
import com.yujinglin.writable.airline;
import com.yujinglin.writable.lowest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class toJson {
    public static void main(String[] args) throws IOException {
        List<lowest> airline = new ArrayList<>();

        // 假设 part-r-00001 文件位于本地文件系统中
        String filePath = "src/main/java/input/outputYu/Output3/part-r-00000";

        try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 这里假设 airlineWritable 有一个接受字符串的构造函数
                // 你需要根据实际情况来填充 airlineWritable 对象
                String[] string = line.split("\\s+");
                airline.add(new lowest(string[0],string[1])); // 假设构造函数存在
            }
        }

        String jsonString = JSON.toJSONString(airline, SerializerFeature.PrettyFormat);
        try (FileWriter fileWriter = new FileWriter("\"C:\\Users\\hanyiyang\\Desktop\\map-reduce-project\\src\\main\\webapp\\word\\mintest.json")) {
            // 将JSON字符串写入文件
            fileWriter.write(jsonString);
            fileWriter.flush();
            System.out.println("airlineWritable objects have been written to 'test.json'");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}